<?php
namespace App\Services\Admin;

use App\Repositories\Admin\OrderBaleRepository;
use App\Repositories\Admin\OrderCheckRepository;
use App\Repositories\Admin\OrderRepository;
use App\Repositories\Admin\CompanyRepository;

class OrderService
{
    private $service;
    private $company;
    private $bale;
    private $check;

    public function __construct()
    {
        $this->service = new OrderRepository();
        $this->company = new CompanyRepository();
        $this->bale = new OrderBaleRepository();
        $this->check = new OrderCheckRepository();
    }

    public function getIndex()
    {
        $models = $this->service->getIndexList();
        if (count($models)) {
            foreach ($models as $model) {
                $company = $this->company->getShowOneByUid($model->cid);
                $factory = $this->company->getShowOneByUid($model->fid);
                $bale = $this->bale->getShowOne($model->bale);
                $model->comName = $company ? $company->com_name : '测试公司';
                $model->facName = $factory ? $factory->com_name : '测试工厂';
                $model->baleName = $bale ? $bale->name : '测试货单';
                $model->periodTime = $bale ? date('Y-m-d',$bale->period) : '测试时间';
            }
        }
        return $models;
    }

    /**
     * 本公司订单
     */
    public function getIndexByCid($uid)
    {
        $models = $this->service->getIndexByCid($uid);
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $company = $this->company->getShowOneByUid($model->fid);
            $bale = $this->bale->getShowOne($model->bale);
            $model->facName = $company ? $company->com_name : '测试公司';
            $model->facId = $company ? $company->uid : 0;
            $model->baleName = $bale ? $bale->name : '测试货单';
            $model->baleImg = $bale ? $bale->img : '';
            $model->periodName = $bale ? date('Y-m-d',$model->period) : '测试时间';
        }
        return $models;
    }

    /**
     * 本工厂加工的
     */
    public function getIndexByFid($uid)
    {
        $models = $this->service->getIndexByFid($uid);
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $company = $this->company->getShowOneByUid($model->cid);
            $bale = $this->bale->getShowOne($model->bale);
            $model->comName = $company ? $company->com_name : '测试公司';
            $model->baleName = $bale ? $bale->name : '测试货单';
            $model->baleImg = $bale ? $bale->img : '';
            $model->periodName = $bale ? date('Y-m-d',$model->period) : '测试时间';
        }
        return $models;
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function getOne($id)
    {
        $model = $this->service->getShowOne($id);
        if (!$model) { return false; }
        $company = $this->company->getShowOneByUid($model->fid);
        $bale = $this->bale->getShowOne($model->bale);
        $model->comName = $company ? $company->com_name : '测试公司';
        $model->comAddress = $company ? $company->com_address : '测试地址';
        $model->leaderName = $company ? $company->legal_name : '测试负责人';
        $model->leaderMobile = $company ? $company->legal_mobile : '测试手机';
        $model->baleName = $bale ? $bale->name : '测试货单';
        $model->orderNum = $bale ? $model->num : 0;
        $model->orderMoney = $bale ? $model->money : 0;
        $model->baleDetail = $bale ? $bale->detail : '测试描述...';
        $model->periodTime = $bale ? date('Y-m-d',$bale->period) : '测试时间';
        return $model;
    }

    /**
     * 状态名称
     */
    public function getStatusName($status)
    {
        return $this->service->getStatusName($status);
    }

    /**
     * 工厂，生产状态，设置
     */
    public function setOrderMakeStatus($orderId)
    {
        $model = $this->service->getShowOne($orderId);
        if (!$model || $model->status!=1) { return false; }
        $this->service->setOrderMakeStatus($orderId);
        return true;
    }





    /**
     * ---------------------------------
     * 下面是跟单报告
     * ---------------------------------
     */

    /**
     * 公司，跟单报告，获取
     */
    public function getComCheck($orderId)
    {
        return $this->check->getShowOneByOrder($orderId);
    }

    /**
     * 公司，跟单报告，设置
     */
    public function setComCheck($orderId,$data)
    {
        $model = $this->check->getShowOneByOrder($orderId);
        if (!$model) {
            $data['order'] = $orderId;
            return $this->check->setAdd($data);
        }
        $this->check->setModify($data,$orderId);
        return true;
    }

    /**
     * 公司，确认验收报告
     */
    public function setComCheckSure($orderId)
    {
        return $this->service->setStatusSure($orderId);
    }
}